package com.hanwuwen.demo.HelloQuartz;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class HelloScheduler {

  public static void main(String[] args) throws SchedulerException {
    //1.创建一个JobDetail实例，将该实例与HelloJob Class绑定
    JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
        .withIdentity("myJob", "group1").build();
    //2.创建一个Trigger实例，定义该job立即执行，并且每隔两秒钟重复执行一次，直到永远
    Trigger trigger = TriggerBuilder.newTrigger()
        .withIdentity("myTrigger", "group1")
        .startNow()
        .withSchedule(
            SimpleScheduleBuilder.simpleSchedule()
            .withIntervalInSeconds(2).repeatForever())
        .build();
    //3.创建Scheduler实例
    SchedulerFactory sfact = new StdSchedulerFactory();
    Scheduler scheduler = sfact.getScheduler();
    scheduler.start();
    //打印当前的时间，格式为 2018-01-01 00:00:00
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println("当前时间：" + sdf.format(date));
    scheduler.scheduleJob(jobDetail, trigger);
  }
}
